﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;


namespace RISMOTAXDP.EmailService
{
    /// <summary>
    /// 守护进程
    /// 用于启动和管理邮件创建线程以及邮件收发线程
    /// </summary>
    public class GuardThread
    {
        /// <summary>
        /// 进程检查定时器
        /// </summary>
        private System.Threading.Timer m_Timer;
        /// <summary>
        /// 邮件创建线程
        /// </summary>
        private MailCreateThread m_MailCreateThread;
        /// <summary>
        /// 邮件收发线程
        /// </summary>
        private MailTransferThread m_MailTransferThread;

        /// <summary>
        /// MailNewsletterProcessThread线程
        /// </summary>
        private MailNewsletterProcessThread m_NewsletterProcessThread;


        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="createThread"></param>
        /// <param name="transferThread"></param>
        public GuardThread(MailCreateThread createThread, MailTransferThread transferThread)
        {
            m_MailCreateThread = createThread;
            m_MailTransferThread = transferThread;
            //1、创建定时器
            m_Timer = new System.Threading.Timer(this.ThreadCheck, null,
                System.Threading.Timeout.Infinite,
                System.Threading.Timeout.Infinite);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="createThread"></param>
        /// <param name="transferThread"></param>
        /// <param name="newsletterProcessThread"></param>
        public GuardThread(MailCreateThread createThread, MailTransferThread transferThread, MailNewsletterProcessThread newsletterProcessThread)
        {
            m_MailCreateThread = createThread;
            m_MailTransferThread = transferThread;
            m_NewsletterProcessThread = newsletterProcessThread;

            //1、创建定时器
            m_Timer = new System.Threading.Timer(this.ThreadCheck, null,
                System.Threading.Timeout.Infinite,
                System.Threading.Timeout.Infinite);
        }

        /// <summary>
        /// 启动守护进程
        /// </summary>
        public void Start()
        {
            m_Timer.Change(0, 60000);
        }

        /// <summary>
        /// 停止守护进程
        /// </summary>
        public void Stop()
        {
            //1、停止定时器并释放资源
            m_Timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
            m_Timer.Dispose();
            //2、停止生成及收发线程
            m_MailCreateThread.Stop();
            m_MailCreateThread.Dispose();
            m_MailTransferThread.Stop();
            m_MailTransferThread.Dispose();
            //3、停止NewsletterProcess线程
            if (m_NewsletterProcessThread != null)
            {
                m_NewsletterProcessThread.Stop();
                m_NewsletterProcessThread.Dispose();
            }
        }

        /// <summary>
        /// 进程检查
        /// </summary>
        /// <param name="obj"></param>
        public void ThreadCheck(object obj)
        {
            lock (this)
            {
                //1、检查后台程序的执行状态
                if (GlobalObject.State == ServiceState.Running)
                {
                    try
                    {
                        if (!GlobalObject.SystemInfo.Initialized)
                        {
                            GlobalObject.SystemInfo.Initialize();
                        }
                        if (GlobalObject.SystemInfo.Initialized && !GlobalObject.DataBaseInfo.Initialized)
                        {
                            GlobalObject.DataBaseInfo.Initialize();
                        }
                        if (GlobalObject.SystemInfo.Initialized && GlobalObject.DataBaseInfo.Initialized)
                        {
                            //2、检查邮件生成线程
                            if (m_MailCreateThread.State == ThreadState.Unstarted || m_MailCreateThread.State == ThreadState.Stopped)
                            {
                                try
                                {
                                    m_MailCreateThread.Start();
                                }
                                catch (Exception ex)
                                {
                                    GlobalObject.WriteOperationLog("E80010003", ex);
                                    m_MailCreateThread.Dispose();
                                    m_MailCreateThread = new MailCreateThread();
                                }
                            }
                            //3、检查邮件收发线程
                            if (m_MailTransferThread.State == ThreadState.Unstarted || m_MailTransferThread.State == ThreadState.Stopped)
                            {
                                try
                                {
                                    m_MailTransferThread.Start();
                                }
                                catch (Exception ex)
                                {
                                    GlobalObject.WriteOperationLog("E80010004", ex);
                                    m_MailTransferThread.Dispose();
                                    m_MailTransferThread = new MailTransferThread();
                                }
                            }

                            //检查NewsletterProcessThread线程
                            if (m_NewsletterProcessThread != null)
                            {                            }
                                if (m_NewsletterProcessThread.State == ThreadState.Unstarted || m_NewsletterProcessThread.State == ThreadState.Stopped)
                                {
                                    try
                                    {
                                        m_NewsletterProcessThread.Start();
                                    }
                                    catch (Exception ex)
                                    {
                                        GlobalObject.WriteOperationLog("E80010005", ex);
                                        m_NewsletterProcessThread.Dispose();
                                        m_NewsletterProcessThread = new MailNewsletterProcessThread();
                                    }

                                
                                }




                        }
                    }
                    catch (Exception ex)
                    {
                        //4、处理函数异常
                        GlobalObject.WriteOperationLog("E80010000", ex);
                    }
                }
            }
        }
    }

}
